<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/*
 * 返回json数据
 * @param int $code 状态码
 * @param string $msg 提示信息
 * @param array $result 结果数据数组
 * @return json
 *
 * */
function rjson($code=100,$msg='',$result=array()){
     
    $a = array(
        'code' => $code,
        'msg' => $msg,
        'result' => $result
    );
    
    echo json_encode($a);
    exit();
}



/*
 * 产生token算法
 * token = md5（$openid + 时间戳 + 5位随机字符串 ）
 *
 * */

function generateToken($openid,$num=5){
    return md5($openid.time().RandomNum($num));
}

/*
 * 生成随机字符串
 * $param int $num 随机字符串的位数
 *
 * */
function RandomNum($num=5){
    $str = "abcdefghijklmnopqrstuvwxyz1234567890";

    $len = strlen($str);
    $randomstr = '';
    for ($i=0;$i<$num;$i++){
        $randomstr.= $str[rand(1,$len)-1];
    }
    
    return $randomstr;


}


//写入配置文件
function setConfigfile($file, $arr){
    $str="<?php \nreturn [\n";
    foreach($arr as $key=>$v){
        /*$v = htmlspecialchars_decode($v);
        $v = htmlspecialchars($v);*/

        $str.= "\t'".$key."'=>'".$v."',\n";
    }
    $str.="];\n";
    file_put_contents($file, $str);
}


function add_slashes_recursive( $variable )
{
    if ( is_string( $variable ) )
        return addslashes( $variable ) ;

    elseif ( is_array( $variable ) )
        foreach( $variable as $i => $value )
            $variable[ add_slashes_recursive( $i ) ] = add_slashes_recursive( $value ) ;

    return $variable ;
}


/*
 * 把时间化为对于0点的分钟数，并且是向下区整5的分钟数
 * @param $date 时间 
 * */
function changDateToMinutes5($date){
    $datemin = date('H:i',strtotime($date)); ;//分钟时间
    $zeromin = '00:00' ;//分钟时间
    $minute=floor((strtotime($datemin)-strtotime($zeromin))%86400/60);
    $minute = $minute - $minute%5;

    return $minute;

}


/*
 *对浮点数进行四舍五入,2位有位小数
 *
 *
 * */
function round2($float){
    if (is_numeric($float)){
        return  number_format(round($float,2),2) ;
    }else{
        return $float;
    }
}

function trimall($str)//删除空格
{
    $oldchar=array(" ","　","\t","\n","\r");
    $newchar=array("","","","","");
    return str_replace($oldchar,$newchar,$str);
} 